/*
用户登陆的路由组件
*/
import React, {Component} from 'react';
import Logo from '../../components/logo/logo';
import {connect} from 'react-redux';
import {Redirect} from 'react-router-dom';
import {login, register} from '../../redux/actions';

import {
    NavBar,
    WingBlank,
    List,
    InputItem,
    WhiteSpace,
    Button
} from 'antd-mobile';

const ListItem = List.Item;

class Login extends Component {
    state = {
        username: '', //用户名
        password: ''  //密码
    }
    login = () => {
        this.props.login(this.state);
    }
    toRegister = () => {
        this.props.history.replace('/register');
    }
    handlerChange = (name, val) => {
        this.setState({
            [name]: val
        });
    }

    render() {
        let {type} = this.state;
        const {msg, redirectTo} = this.props.user;
        //如果redirectTo有值，就需要重定向到指定的路径去
        if (redirectTo) {
            return <Redirect to={redirectTo}/>
        }
        return (
            <div>
                <NavBar>硅&nbsp;谷&nbsp;直&nbsp;聘</NavBar>
                <Logo/>
                <WingBlank>
                    <List>
                        {msg ? <div className='error-msg'>{msg}</div> : null}
                        <InputItem placeholder='请输入用户名' onChange={val => {
                            this.handlerChange('username', val)
                        }}>用户名：</InputItem>
                        <WhiteSpace/>
                        <InputItem placeholder='请输入密码' type="password" onChange={val => {
                            this.handlerChange('password', val)
                        }}>密码：</InputItem>
                        <WhiteSpace/>
                        <Button type="primary" onClick={this.login}>登陆</Button>
                        <WhiteSpace/>
                        <Button onClick={this.toRegister}>还没有账户</Button>
                    </List>
                </WingBlank>
            </div>
        )
    }
}

export default connect(
    state => ({user: state.user}),
    {login}
)(Login)